package com.example.anliku.service.serviceImpl;

import com.example.anliku.entity.*;
import com.example.anliku.entity.clazzBody.clazzRetBody;
import com.example.anliku.entity.clazzBody.clazzRetGxBody;
import com.example.anliku.entity.knowledgeBody.knowledgeRetBody;
import com.example.anliku.entity.knowledgeBody.knowledgeRetGxBody;
import com.example.anliku.mapper.KnowledgeMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.checkerframework.checker.units.qual.K;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.example.anliku.mapper.ExampleKnowledgeMapper;
import com.example.anliku.service.ExampleKnowledgeService;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExampleKnowledgeServiceImpl implements ExampleKnowledgeService {

    @Resource
    private ExampleKnowledgeMapper exampleKnowledgeMapper;

    @Resource
    private KnowledgeMapper knowledgeMapper;

    @Override
    public Message deleteByPrimaryKey(Integer exampleKnowledgeId) {
        return exampleKnowledgeMapper.deleteByPrimaryKey(exampleKnowledgeId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public int insert(ExampleKnowledge record) {
        return exampleKnowledgeMapper.insert(record);
    }

    @Override
    public int insertSelective(ExampleKnowledge record) {
        return exampleKnowledgeMapper.insertSelective(record);
    }

    @Override
    public ExampleKnowledge selectByPrimaryKey(Integer exampleKnowledgeId) {
        return exampleKnowledgeMapper.selectByPrimaryKey(exampleKnowledgeId);
    }

    @Override
    public int updateByPrimaryKeySelective(ExampleKnowledge record) {
        return exampleKnowledgeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ExampleKnowledge record) {
        return exampleKnowledgeMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message insertList(List<ExampleKnowledge> list) {
        for (ExampleKnowledge exampleKnowledge : list) {
            Knowledge knowledge = new Knowledge();
            knowledge.setKnowledgeName(exampleKnowledge.getExampleKnowledgeName());
            try {
                knowledgeMapper.insert(knowledge);
            } catch (Exception e) {
            }
        }
        return exampleKnowledgeMapper.insertList(list) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message batchInsertBody(List<knowledgeRetBody> list) {
        List<Knowledge> knowledgeArrayList = new ArrayList<>();
        for (knowledgeRetBody knowledgeRetBody : list) {
            if(knowledgeRetBody.getKnowledgeName() == null){
                return Message.fail("知识点为空");
            }
            List<Knowledge> a = knowledgeMapper.selectAllByKnowledgeName(knowledgeRetBody.getKnowledgeName());
            if (CollectionUtils.isNotEmpty(a)) {
                return Message.fail("知识点添加重复");
            }
            knowledgeArrayList.add(Knowledge.get(knowledgeRetBody));
        }
        return knowledgeMapper.batchInsert(knowledgeArrayList) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message batchInsertGx(List<knowledgeRetGxBody> list) {
        //exampleClazzMapper
        List <ExampleKnowledge> exampleKnowledgeList=new ArrayList();
        for(knowledgeRetGxBody knowledgeRetGxBody:list){
            List<ExampleKnowledge> aa= exampleKnowledgeMapper.selectAllByExampleKnowledgeNameAndExampleKnowledgeExampleId(knowledgeRetGxBody.getExampleKnowledgeName(),knowledgeRetGxBody.getExampleKnowledgeExampleId());
            if(knowledgeRetGxBody.getExampleKnowledgeName() == null){
                return Message.fail("知识点为空");
            }
            if(CollectionUtils.isEmpty(aa)){//没重复
                List<Knowledge> knowledgeList=knowledgeMapper.selectAllByKnowledgeName(knowledgeRetGxBody.getExampleKnowledgeName());//看课程名是否重复
                ExampleKnowledge exampleKnowledge=new ExampleKnowledge();
                if(CollectionUtils.isEmpty(knowledgeList)){//没重复就加上
                    Knowledge knowledge = new Knowledge();
                    knowledge.setKnowledgeName(knowledgeRetGxBody.getExampleKnowledgeName());
                    knowledgeMapper.insert(knowledge);
                }
                exampleKnowledge.setExampleKnowledgeExampleId(knowledgeRetGxBody.getExampleKnowledgeExampleId());
                exampleKnowledge.setExampleKnowledgeName(knowledgeRetGxBody.getExampleKnowledgeName());
                exampleKnowledgeList.add(exampleKnowledge);
            }else{//案例关系重复
                return Message.fail("知识点与案例对应关系重复");
            }

        }
        return exampleKnowledgeMapper.batchInsert(exampleKnowledgeList) > 0 ? Message.success(null) : Message.fail(null);
    }


}
